package books4africa

import static org.junit.Assert.*
import grails.test.*
import org.junit.*

class BookRequestIntegrationTests {
	
	private User newUser(){
		return new User(
			username:'jjohns',
			password:'secretOne',
			fullName:'Jason Johns',
			email:'jjohns@maine.edu',
			//country:"United States",
			//address:"United States",
			userType:'Teacher',
			city:"Biddeford",
			schoolName:"USM",
			gradeLevel:"6",
		)
	}
	
	private Books newBook(){
		return new Books(
			title:"TestBookOne",
			author:"Me",
			category:"Health",
			minGradeLevel:"2",
			maxGradeLevel: "6",
		)
	}
	
	private BookRequest newRequest(newBook, newUser){
		return new BookRequest(
			user:newUser,
			description:"New Book Request from Me",
			book:newBook,
			dateRequested: new Date()
		)
	}
	
	void testNewRequest(){
		
		def newUser = newUser().save(failOnError:true)
		def book = newBook().save(flush:true, failOnError:true)
		def newRequest = newRequest(newUser, book).save(failOnError:true)
		
		def foundRequest = BookRequest.get(newRequest.id)
		assertEquals newUser, newRequest.user
	}
	
	void testUpdateRequest(){
		def newUser= new User(
			username:'jjohns',
			password:'secretOne',
			fullName:'Jason Johns',
			email:'jjohns@maine.edu',
			//country:"United States",
			//address:"United States",
			userType:'Teacher',
			city:"Biddeford",
			schoolName:"USM",
			gradeLevel:"6",
		).save(failOnError:true)
		
		def newBook = new Books(
			title:"TestBookOne",
			author:"Me",
			category:"Health",
			minGradeLevel:"2",
			maxGradeLevel: "6",
		).save(failOnError:true)
		
		def newRequest = new BookRequest(
			user:newUser,
			description:"New Book Request from Me",
			book:newBook,
			dateRequested: new Date()
		).save(failOnError:true)
		
		def foundRequest = BookRequest.get(newRequest.id)
		foundRequest.user.fullName="Brad Pare"
		foundRequest.save(failOnError:true)
		
		def editedRequest = BookRequest.get(newRequest.id)
		assertEquals "Brad Pare", newRequest.user.fullName
	}
	
	void testRequestDelete(){
		def newUser= new User(
			username:'jjohns',
			password:'secretOne',
			fullName:'Jason Johns',
			email:'jjohns@maine.edu',
			//country:"United States",
			//address:"United States",
			userType:'Teacher',
			city:"Biddeford",
			schoolName:"USM",
			gradeLevel:"6",
		).save(failOnError:true)
		
		def newBook = new Books(
			title:"TestBookOne",
			author:"Me",
			category:"Health",
			minGradeLevel:"2",
			maxGradeLevel: "6",
		).save(failOnError:true)
		
		def newRequest = new BookRequest(
			user:newUser,
			description:"New Book Request from Me",
			book:newBook,
			dateRequested: new Date()
		).save(failOnError:true)
		
		def foundRequest = BookRequest.get(newRequest.id)
		foundRequest.delete()
		
		assertFalse BookRequest.exists(newRequest.id)
	}

}
